
package com.jiangxiaofei.citycloud.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jiangxiaofei.citycloud.R;


/**
 * 加载dialog TODO
 *
 * @author lml
 * @2015-5-27
 * @ProgressDialogHelper
 */
public class ProgressDialogHelper extends Dialog {

    private Context context = null;

    private static ProgressDialogHelper customProgressDialog = null;

    private RelativeLayout rl;

    private static MyTextView mTv_title;

    public ProgressDialogHelper(Context context) {
        super(context);
        this.context = context;
    }

    public ProgressDialogHelper(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        // rl = (RelativeLayout) findViewById(R.id.rl_dialog);
        // rl.setMinimumWidth((int) (Constants.width));
        // rl.setMinimumHeight((int) (Constants.height));
    }

    /**
     * 显示
     *
     * @TODO 2015-5-27 void
     */
    public static void show(Context context, String msg) {
        try {
            customProgressDialog = new ProgressDialogHelper(context, R.style.CustomProgressDialog);
            View view = LayoutInflater.from(context).inflate(R.layout.app_layout_loaddialog_view, null);
            mTv_title = (MyTextView) view.findViewById(R.id.id_tv_loadingmsg);
            mTv_title.setText(msg);
            // WindowManager.LayoutParams lp = customProgressDialog.getWindow()
            // .getAttributes();
            // lp.alpha = 0.3f;
            // customProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            // customProgressDialog.getWindow().setAttributes(lp);
            // 透明度，黑暗度为lp.dimAmount=1.0f;
            customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
            customProgressDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
            customProgressDialog.setContentView(view);

            if (customProgressDialog == null) {
                customProgressDialog.setMessage(msg);
            }
            if (!customProgressDialog.isShowing()) {
                customProgressDialog.show();
            }
        } catch (Exception e) {

        }
    }

    /**
     * 显示
     *
     * @TODO 2015-5-27 void
     */
    public static void show(Context context) {
        customProgressDialog = new ProgressDialogHelper(context, R.style.CustomProgressDialog);
        View view = LayoutInflater.from(context).inflate(R.layout.app_layout_loaddialog_view, null);

        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        customProgressDialog.setContentView(view);
        customProgressDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        if (!customProgressDialog.isShowing()) {
            customProgressDialog.show();
        }
    }

    /**
     * 显示
     *
     * @TODO 2015-5-27 void
     */
    // public static void showInfo(Context context, String msg) {
    //
    // customProgressDialog = new ProgressDialogHelper(context,
    // R.style.rob_Dialog);
    //
    // WindowManager.LayoutParams lp =
    // customProgressDialog.getWindow().getAttributes();
    // lp.alpha = 0.5f;
    // customProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    // customProgressDialog.getWindow().setAttributes(lp);
    // // View dialog_view = LayoutInflater.from(context).inflate(
    // // R.layout.dialog_rob_view, null);
    // // TextView load = (TextView)
    // // dialog_view.findViewById(R.id.tv_loadingmsg);
    // // load.setText(msg);
    // // // 透明度，黑暗度为lp.dimAmount=1.0f;
    // // customProgressDialog.setContentView(dialog_view);
    //
    // customProgressDialog.getWindow().getAttributes().gravity =
    // Gravity.CENTER;
    // if (customProgressDialog == null) {
    // customProgressDialog.setMessage(msg);
    // }
    // if (!customProgressDialog.isShowing()) {
    // customProgressDialog.show();
    // }
    //
    // }
    static OnKeyListener keylistener = new OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                return true;
            } else {
                return false;
            }
        }
    };

    /**
     * 关闭
     *
     * @TODO 2015-5-27 void
     */
    public static void dismissDialog() {
        try {
            if (customProgressDialog != null) {
                customProgressDialog.dismiss();
                customProgressDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void onWindowFocusChanged(boolean hasFocus) {

        if (customProgressDialog == null) {
            return;
        }

        ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
    }

    /**
     * [Summary] setTitile 标题
     *
     * @param strTitle
     * @return
     */
    public ProgressDialogHelper setTitile(String strTitle) {
        return customProgressDialog;
    }

    /**
     * [Summary] setMessage 提示内容
     *
     * @param strMessage
     * @return
     */
    public ProgressDialogHelper setMessage(String strMessage) {
        MyTextView tvMsg = (MyTextView) customProgressDialog.findViewById(R.id.id_tv_loadingmsg);

        if (tvMsg != null) {
            tvMsg.setText(strMessage);
        }
        return customProgressDialog;
    }

}
